package com.joy.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.joy.servicebase.exceptionHandler.GuliException;
import com.joy.vod.service.VodService;
import com.joy.vod.utils.ConstantVodUtils;
import com.joy.vod.utils.InitObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 焦恩越
 * @create 2021-12-04-16:38
 */
@Service
public class VodServiceImpl implements VodService {
    //上传视频到阿里云
    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
//        accessKeyId,accessKeySecret,

//        fileName:上传文件原始名称
            String fileName = file.getOriginalFilename();
//        title:上传之后显示的名称
            String title = fileName.substring(0,fileName.lastIndexOf("."));
//        inputStream：上传文件输入流
            InputStream inputStream = file.getInputStream();
            //
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);


            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);

            String videoId = null;
            if(response.isSuccess()){
                videoId = response.getVideoId();
            }else{
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //删除阿里云视频
    @Override
    public void deleteVideoById(String id) {
        try {
            //初始化
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id();
            request.setVideoIds(id);
            //调用初始化对象实现删除
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }

    //删除阿里云里面的多个视频
    @Override
    public void removeAlyVideo(List videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //将数组中的值转换成，隔开的形式
            String str = StringUtils.join(videoIdList.toArray(), ",");
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id();
            request.setVideoIds(str);
            //调用初始化对象实现删除
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }
}
